/**
 * 
 */
package cc.telecomdigital.tdstock.activity.groups.more.push.client;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import cc.telecomdigital.tdstock.CommonDefn;
import cc.telecomdigital.tdstock.ITDLApplication;
import cc.telecomdigital.tdstock.Framework.UniqueItem.TDStockLog;
import cc.telecomdigital.tdstock.Services.TDStockReqFreeVerify.ITDStockReqFreeVerify_Result;
import cc.telecomdigital.tdstock.Services.TDStockReqFreeVerify.TDStockReqFreeVerify_Result;
import cc.telecomdigital.tdstock.activity.groups.more.push.util.NotificationSwitch;

/**
 * 
 * Project：tdstock3
 * Package：cc.telecomdigital.tdstock.activity.groups.more.push.client
 * Class：AutoStartReceiver Description： Creater：hunter CreateTime：Oct 25, 2011
 * 3:51:27 PM Modifier ：hunter Modify time：Oct 25, 2011 3:51:27 PM Modify
 * Remarks：
 * 
 * @version
 * 
 */
public class AutoStartReceiver extends BroadcastReceiver
{
	private static final String	TAG		= AutoStartReceiver.class
											.getSimpleName();

	private static final String	ACTION	= "android.intent.action.BOOT_COMPLETED";

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.content.BroadcastReceiver#onReceive(android.content.Context,
	 * android.content.Intent)
	 */
	@Override
	public void onReceive(Context context, Intent intent)
	{
		
		TDStockLog.d(TAG, intent.getAction());
		if (intent.getAction().equals(ACTION))
		{
			SharedPreferences _prefs = context.getSharedPreferences(
					CommonDefn.SYSTEM_CURRENT_SETTING,
					Context.MODE_PRIVATE);
			boolean isAutoLogin = _prefs.getBoolean(
					CommonDefn.PREFERENCE_IS_AUTO_LOGIN_APP, false);
			String user = _prefs.getString(
					CommonDefn.PREFERENCE_LAST_ACCOUNT_NAME, "");
			String pwd = _prefs.getString(
					CommonDefn.PREFERENCE_LAST_ACCOUNT_PWD, "");
			boolean isSendActiveCode = _prefs.getBoolean(
					CommonDefn.PREFERENCE_IS_SEND_ACTIVE_CODE, false);
			String activeCode = _prefs.getString(
					CommonDefn.PREFERENCE_TRIAL_ACTIVE_CODE, "");
			// 启动push服务
			if (isAutoLogin)
			{
				if (!"".equals(user) && !"".equals(pwd))
				{
					// TODO  Do more test here
					NotificationSwitch.getInstace().onSwitch(context);
				}
				else if (isSendActiveCode && !"".equals(activeCode))
				{
					// TODO  Do more test here
					TrialQuery tq = new TrialQuery(context);
					tq.execute(activeCode);
				}

			}

		}
	}

	private class TrialQuery extends AsyncTask<String, Void, Void>
	{
	//	private String[]	auth_info;
		//private String activeCode;
		private Context	context;

		public TrialQuery(Context context)
		{
			//this.activeCode = activeCode;
			this.context = context;
		}

		@Override
		protected Void doInBackground(String... params)
		{
			/*String deviceId = context.getSharedPreferences(
					CommonDefn.SYSTEM_CURRENT_SETTING,
					Context.MODE_PRIVATE).getString(
					CommonDefn.PREFERENCE_DEVICE_ID, "");
			if (deviceId.trim().length() < SystemInfo.VALID_DEVICEID_LENGTH)
			{
				deviceId = SystemInfo.getDeviceId(context);
			}*/
			
			
			ITDLApplication  app = (ITDLApplication)context.getApplicationContext();

			/*TrialAccountAuth auth = new TrialAccountAuth(deviceId);
			auth_info = auth.getAuthentication();*/
			
			TrialQueryAuth(params[0], app, new Runnable() {
				public void run() {
					NotificationSwitch.getInstace().onSwitch(context);
				}
			});
			
			return null;
		}

		protected void onPostExecute(Void result)
		{
			super.onPostExecute(result);
			/*if (auth_info[0].equals("0"))
			{
				NotificationSwitch.getInstace().onSwitch(context);
			}*/
		}
	}
	
	
	
	private void TrialQueryAuth(final String iActivateCode, ITDLApplication appHost , final Runnable runnableForPass )
	{
		appHost.LS_RequestFreeTrialVerifiy(new ITDStockReqFreeVerify_Result()
		{
			@Override
			public void TDStockReqFreeVerify_Response(TDStockReqFreeVerify_Result iResult)
			{
				if (iResult.successfulSend && "0".equals(iResult.statusCode))
				{
					runnableForPass.run();
				}
				else
				{
					
				}
			}
		});
	}
	
	
	
	
}
